//
//  NSDictionary+YC.m
//
//
//  Created by Yanglixia on 16/5/6.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "NSDictionary+YC.h"

@implementation NSDictionary (YC)

- (void)logPropertyCode
{
    NSMutableString *propertyCode = [NSMutableString string];
    
    // 根据字典生成对应的属性代码
    // 遍历字典中所有key,生成对应的属性代码
    [self enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        
        NSString *code ;
        // 判断真实类型,才能确定属性代码
        if ([value isKindOfClass:[NSString class]]) {
           code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSString *%@;",key];
        } else if ([value isKindOfClass:[NSDictionary class]]){
             code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSDictionary *%@;",key];
        } else if ([value isKindOfClass:[NSArray class]]){
            code = [NSString stringWithFormat:@"@property(nonatomic, strong) NSArray *%@;",key];
        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            code = [NSString stringWithFormat:@"@property(nonatomic, assign) BOOL %@;",key];
        } else if ([value isKindOfClass:[NSNumber class]]){
            code = [NSString stringWithFormat:@"@property(nonatomic, assign) NSInteger %@;",key];
        }
        
        // 拼接一次
        [propertyCode appendFormat:@"\n%@\n",code];
        
    }];
    
    YCLog(@"%@",propertyCode);
}
@end

/************************************* 中文打印输出  ***********************************/
@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *outStr = [NSMutableString string];
    //打印"{"
    [outStr appendFormat:@"{ \n"];
    
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        [outStr appendFormat:@"\t %@ : %@ ,\n",key,obj];
        
    }];
    //去除最后一个逗号
    NSRange range = [outStr rangeOfString:@"," options:NSBackwardsSearch];
    if (range.location != NSNotFound) {
        
        [outStr deleteCharactersInRange:range];
    }
    
    [outStr appendFormat:@"}"];
    return outStr;
}

@end

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *outStr = [NSMutableString string];
    //打印"["
    [outStr appendFormat:@"[ "];
    
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [outStr appendFormat:@"%@ ,\n",obj];
    }];
    
    //去除最后一个逗号
    NSRange range = [outStr rangeOfString:@"," options:NSBackwardsSearch];
    if (range.location != NSNotFound) {
        
        [outStr deleteCharactersInRange:range];
    }
    [outStr appendString:@"]"];
    
    
    return outStr;
    
}

@end
